﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using Milkify.Diagnostics;

namespace Milkify.Input
{
    public static class FocusHelper
    {
        public static bool FocusFirstItem(ItemsControl itemsControl)
        {
            if (itemsControl.Items.Count > 0)
            {
                IInputElement element = itemsControl.ItemContainerGenerator.ContainerFromIndex(0) as IInputElement;
                if (element != null)
                {
                    if (element.Focus())
                    {
                        MilkifyTraceSources.UISource.TraceEvent(TraceEventType.Verbose, 0, "Successfully focused first list element '{0}'.", element);
                        return true;
                    }
                }
            }
            return false;
        }

        public static bool FocusFirstItem(ItemsControl itemsControl, string elementName)
        {
            if (itemsControl.Items.Count > 0)
            {
                DependencyObject firstItemContainer = itemsControl.ItemContainerGenerator.ContainerFromIndex(0);
                if (firstItemContainer != null)
                {
                    ContentPresenter firstItemPresenter = FindVisualChild<ContentPresenter>(firstItemContainer);
                    if (firstItemPresenter != null)
                    {
                        firstItemPresenter.ApplyTemplate();
                        DataTemplate itemTemplate = firstItemPresenter.ContentTemplate;
                        IInputElement element = (IInputElement)itemTemplate.FindName(elementName, firstItemPresenter);
                        if (element != null)
                        {
                            if (element.Focus())
                            {
                                MilkifyTraceSources.UISource.TraceEvent(TraceEventType.Verbose, 0, "Successfully focused first list element '{0}'.", elementName);
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        }

        public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
        {
            if (obj is T)
                return (T)obj;

            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is T)
                    return (T)child;
                else
                {
                    T childOfChild = FindVisualChild<T>(child);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }
    }
}
